import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import pickle
from time import time

begin = time()

# Functions ------------------------------------------------------------------------------------------------------------
def loadDLosses(savePath_DL1, savePath_DL2, savePath_LL):
    DL1 = np.load(savePath_DL1)
    DL2 = np.load(savePath_DL2)
    LL = np.load(savePath_LL)
    return DL1, DL2, LL

def computeDScores(DL1, DL2):    
    DL1 = (-1) * DL1
    DL2 = (-1) * DL2
    drScores = []
    dr_score = DL1
    drScores.append(dr_score)
    dr_score = DL2
    drScores.append(dr_score)
    return drScores

def computeAucRocs(LL, drScores):
    aucRocMax = 0
    aucRocMaxIndex = 0
    index = 0
    for dr_score in drScores:
        aucRoc = roc_auc_score(LL, dr_score)
        print("aucRoc: ", aucRoc)
        if(aucRocMax < aucRoc):
            aucRocMax = aucRoc
            aucRocMaxIndex = index
        index = index + 1
    print("aucRocMaxIndex: ", aucRocMaxIndex)
    print("aucRocMax: ", aucRocMax)
    return aucRocMaxIndex, aucRocMax

def plotAucRoc(LL, drScores, aucRocMaxIndex, savePathPlotAucRoc):
    fpr, tpr, thresholds = metrics.roc_curve(LL, drScores[aucRocMaxIndex], pos_label=1)
    plt.figure(1)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr, label='ROC Curve')
    plt.xlabel('False positive rate')
    plt.ylabel('True positive rate')
    plt.title('ROC curve')
    plt.legend(loc='best')
    plt.show()
    plt.savefig(savePathPlotAucRoc)

#-----------------------------------------------------------------------------------------------------------------------

# Main Executtion ------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
    print('Main Starting...')
    
    aucRocMaxEpochsGAN = 0
    aucRocMaxEpochsGANIndex = 0
    savePathPart1 = "./Experiments_Autoencoder2/Discriminator_Only/D_Losses_EpochsGAN/D_Losses_"
    for epoch_GAN in range(0, 100):
        print("\nepoch_GAN: ", epoch_GAN)
        savePath_DL1 = savePathPart1 + str(epoch_GAN) + "/DL1_" + ".npy"
        savePath_DL2 = savePathPart1 + str(epoch_GAN) + "/DL2_" + ".npy"
        savePath_LL = savePathPart1 + str(epoch_GAN) + "/LL_" + ".npy"
        DL1, DL2, LL = loadDLosses(savePath_DL1, savePath_DL2, savePath_LL)
        drScores = computeDScores(DL1, DL2)
        aucRocMaxIndex, aucRocMax = computeAucRocs(LL, drScores)
        if(aucRocMaxEpochsGAN < aucRocMax):
            aucRocMaxEpochsGAN = aucRocMax
            aucRocMaxEpochsGANIndex = epoch_GAN

    print("\n\naucRocMaxEpochsGAN: ", aucRocMaxEpochsGAN)
    print("aucRocMaxEpochsGANIndex: ", aucRocMaxEpochsGANIndex)

    # savePathPlotAucRoc = "./Experiments_Autoencoder/Autoencoder_Loss_G/D_R_Losses_EpochsGAN/Auc_ROC_Plots/ROC_Curve.png"
    # plotAucRoc(LL, drScores, aucRocMaxIndex, savePathPlotAucRoc)

    print('Main Terminating...')
    end = time() - begin
    print('Testing terminated | Execution time=%d s' % (end))
#-----------------------------------------------------------------------------------------------------------------------

